package com.fhnew.app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.ResGetPay;
import com.fhnew.app.model.network.entity.req.ResPayList;
import com.fhnew.app.ui.dialogactivity.AlipayctivitySm;
import com.fhnew.app.ui.dialogactivity.BankTransfer;
import com.fhnew.app.ui.dialogactivity.MyWebviewUtls;
import com.fhnew.app.ui.dialogactivity.SetingActivity;
import com.fhnew.app.util.ActivityUtil;
import com.fhnew.app.util.OnItemClickListener;
import com.fhnew.app.util.SYSOUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 充值
 */
public class AddMoneyActivity extends AppCompatActivity {

    private ImageView reback;
    private EditText addmoneyet;
    private TextView addmoneytext;
    private TextView addmoneybt;
    private TextView tiptext;
    private ImageView shezhi;
    private RecyclerView addmoneylist;
    private RecyclerView addmoneyuplist;

    private ArrayList<ResPayList.RechTypesBean> rechTypesBeans = new ArrayList<>();
    private ArrayList<ResGetPay.RechTypesBean> resgetpay = new ArrayList<>();
    private Adapter adapter;
    private Adapter1 adapter1;
    private int onclickpostion = 0;
    private int postion2 = 0;

    private TextView money1;
    private TextView money2;
    private TextView money3;
    private TextView money4;
    private TextView money5;
    private TextView money6;
    private TextView money7;
    private TextView money8;
    private TextView addmoneyclear;
    private String czid;
    private LinearLayout skinbg;
    private int xiaxian = 0;
    private int shangxian = 0;
    private String pay_online_three;//支付名称
    private String pay_code;//支付leixing



    private void initView(){


         reback= findViewById(R.id.reback);
         addmoneyet= findViewById(R.id.addmoneyet);
         addmoneytext= findViewById(R.id.addmoneytext);
         addmoneybt= findViewById(R.id.addmoneybt);
         tiptext= findViewById(R.id.tiptext);
         shezhi= findViewById(R.id.shezhi);
         addmoneylist= findViewById(R.id.addmoneylist);
         addmoneyuplist= findViewById(R.id.addmoneyuplist);

         money1= findViewById(R.id.money1);
         money2= findViewById(R.id.money2);
         money3= findViewById(R.id.money3);
         money4= findViewById(R.id.money4);
         money5= findViewById(R.id.money5);
         money6= findViewById(R.id.money6);
         money7= findViewById(R.id.money7);
         money8= findViewById(R.id.money8);
         addmoneyclear = findViewById(R.id.addmoneyclear);

        reback.setOnClickListener(onClickListener);
        shezhi.setOnClickListener(onClickListener);
        money1.setOnClickListener(onClickListener);
        money2.setOnClickListener(onClickListener);
        money3.setOnClickListener(onClickListener);
        money4.setOnClickListener(onClickListener);
        money5.setOnClickListener(onClickListener);
        money6.setOnClickListener(onClickListener);
        money7.setOnClickListener(onClickListener);
        money8.setOnClickListener(onClickListener);
        addmoneyclear.setOnClickListener(onClickListener);


        getaddmoneylist();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        addmoneylist.setLayoutManager(layoutManager);
        adapter = new Adapter();
        addmoneylist.setAdapter(adapter);

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        addmoneyuplist.setLayoutManager(layoutManager1);
        adapter1 = new Adapter1();
        addmoneyuplist.setAdapter(adapter1);

        addmoneybt.setOnClickListener(onClickListenerAdd);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityUtil.fullScreen(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_money);

        initView();
        initInfo();
    }


    @Override
    protected void onDestroy () {
        super.onDestroy();
        userModel.unRegistListener(userInformationChanged);
    }

    private View.OnClickListener onClickListenerAdd =new View.OnClickListener() {
        @Override
        public void onClick (View v) {
            try {
                if (pay_online_three.equals("请点击选择支付账号")) {
                    Toast.makeText(AddMoneyActivity.this, "请点击选择支付账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (addmoneyet.getText().toString().isEmpty()) {
                    Toast.makeText(AddMoneyActivity.this, "请输入金额", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (Double.parseDouble(addmoneyet.getText().toString()) < xiaxian) {
                    Toast.makeText(AddMoneyActivity.this, "输入金额不能少于" + xiaxian, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (Double.parseDouble(addmoneyet.getText().toString()) > shangxian) {
                    Toast.makeText(AddMoneyActivity.this, "输入金额不能高于" + shangxian, Toast.LENGTH_SHORT).show();
                    return;
                }
                String url = Constants.getuseAdress() + "api/m/pay/paymentSubmission" + "?id=" + czid + "&money=" +
                        addmoneyet.getText().toString() + "&token=" + UserModel.user.token;
//                SYSOUtils.sOut_("名称+++++++++++++++++++++++" + pay_online_three);
//                SYSOUtils.sOut_("最高+++++++++++++++++++++++" + shangxian);
//                SYSOUtils.sOut_("最低+++++++++++++++++++++++" + xiaxian);
                SYSOUtils.sOut_("" + url);
                switch (pay_code) {
                    //                         WY********网银在线支付
//                         YLONLINE********银联快捷
//                         bankTransfer********银行卡转账
//                         ZFB********支付宝在线支付
//                         WX********微信在线支付++++++++++++++++
//                         YL********银联扫码
//                         alipay********支付宝转账
//                         weixin********微信转账
//                         cft********财付通转账
//                         JD********京东钱包
                    //在线支付
                    case "BD":
                    case "JD":
                    case "onlinePayment":
                    case "QQ":
                    case "WX":
                    case "WY":
                    case "XYK":
                    case "YL":
                    case "YLONLINE":
                    case "ZFB":
                        Intent intent = new Intent(AddMoneyActivity.this, MyWebviewUtls.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("url", url);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        break;
                    case "alipay":
                    case "alipaySm":
                    case "cft":
                    case "weixin":
                    case "weixinSm":
                    case "ysf":
//                            Z_alipayctivitySm
                        Intent alipay = new Intent(AddMoneyActivity.this, AlipayctivitySm.class);
                        alipay.putExtra("name", resgetpay.get(postion2).payeeName);
                        alipay.putExtra("paycode", resgetpay.get(postion2).payee);
                        alipay.putExtra("money", addmoneyet.getText().toString());
                        alipay.putExtra("img", resgetpay.get(postion2).qrcode);
                        alipay.putExtra("img2", resgetpay.get(postion2).illustrate);
                        alipay.putExtra("cfgId", resgetpay.get(postion2).id + "");
                        startActivity(alipay);
                        break;
                    case "bankTransfer":
//                            Z_bankTransferctivity
                        Intent bank = new Intent(AddMoneyActivity.this, BankTransfer.class);
                        bank.putExtra("name", resgetpay.get(postion2).payeeName);
                        bank.putExtra("paycode", resgetpay.get(postion2).payee);
                        bank.putExtra("money", addmoneyet.getText().toString());
                        bank.putExtra("img", resgetpay.get(postion2).qrcode);
                        bank.putExtra("img2", resgetpay.get(postion2).illustrate);
                        bank.putExtra("pageDesc", resgetpay.get(postion2).pageDesc);
                        bank.putExtra("cfgId", resgetpay.get(postion2).id + "");
                        startActivity(bank);
                        break;

                    default:
                        Toast.makeText(AddMoneyActivity.this, "android暂未添加该支付", Toast.LENGTH_SHORT).show();
                        break;
                }

            } catch (Exception e) {
                Toast.makeText(AddMoneyActivity.this, "出现错误，请重新操作", Toast.LENGTH_SHORT).show();
            }

        }
    };

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick (View view) {
            switch (view.getId()) {
                case R.id.reback:
                    finish();
                    break;
                case R.id.shezhi:
                    Intent intent = new Intent(AddMoneyActivity.this, SetingActivity.class);
                    startActivity(intent);
                    break;
                case R.id.money1:
                    addmoneyet.setText("50");
                    break;
                case R.id.money2:
                    addmoneyet.setText("100");
                    break;
                case R.id.money3:
                    addmoneyet.setText("300");
                    break;
                case R.id.money4:
                    addmoneyet.setText("500");
                    break;
                case R.id.money5:
                    addmoneyet.setText("800");
                    break;
                case R.id.money6:
                    addmoneyet.setText("1000");
                    break;
                case R.id.money7:
                    addmoneyet.setText("2000");
                    break;
                case R.id.money8:
                    addmoneyet.setText("3000");
                    break;
                case R.id.addmoneyclear:
                    addmoneyet.setText("");
                    break;
            }
        }
    };

    private void getaddmoneylist() {
        HttpJsonUtils.getHttpApiInstance()
                .getPayList(Constants.getuseAdress() + "api/m/user/getPayList", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResPayList>() {
                    @Override
                    protected void onCustomNext(ResPayList resPayList) {
                        if (resPayList.code == 0) {
                            rechTypesBeans.clear();
                            rechTypesBeans.addAll(resPayList.rech_types);
                            adapter.notifyDataSetChanged();
                            getPay(rechTypesBeans.get(0).payCode);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }

    private UserModel.UserInformationChanged userInformationChanged =new UserModel.UserInformationChanged() {
        @Override
        public void userInformationChanged () {
            if(!userModel.isLogin()){
                finish();
            }
        }
    };


    private UserModel userModel;
    private void initInfo(){
        userModel = UserModel.getInstance(this);
        userModel.registListener(userInformationChanged);
    }



    //==========================================================================
    // Adapter
    //==========================================================================

    /////////////////////////////////左侧列表/////////////////////////////////////////////
    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public MViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
            return new MViewHolder(LayoutInflater.from(AddMoneyActivity.this)
                    .inflate(R.layout.addmoneyitem, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            holder.myusercode.setText(rechTypesBeans.get(position).name);
            switch (rechTypesBeans.get(position).payCode) {
                case "alipay":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_alipay);
                    break;
                case "alipaySm":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_alipay);
                    break;
                case "bankTransfer":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_banktransfer);
                    break;
                case "BD":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_bdd);
                    break;
                case "cft":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_cft);
                    break;
                case "JD":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_jd);
                    break;
                case "onlinePayment":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_onlinepayment);
                    break;
                case "QQ":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_qq);
                    break;
                case "weixin":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_weixin);
                    break;
                case "weixinSm":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_weixin);
                    break;
                case "WX":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_wx);
                    break;
                case "WY":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_wy);
                    break;
                case "XYK":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_xyk);
                    break;
                case "YL":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_yl);
                    break;
                case "YLONLINE":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_ylonline);
                    break;
                case "ZFB":
                    holder.payimg.setBackgroundResource(R.mipmap.pay_zfb);
                    break;
                case "ysf":
                    holder.payimg.setBackgroundResource(R.mipmap.ysf);
                    break;
                default:
            }
            if (onclickpostion == position) {
                holder.addmoneylayout.setBackgroundResource(R.mipmap.gamedown);
            } else {
                holder.addmoneylayout.setBackgroundResource(R.mipmap.gameup);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    postion2 = 0;
                    onclickpostion = position;
                    adapter.notifyDataSetChanged();
                    //获取充值方式
                    getPay(rechTypesBeans.get(position).payCode);

                }
            });

        }

        @Override
        public int getItemCount() {
            return rechTypesBeans.size();

        }

        @Override
        public void onItemClick(View view, int position) {

        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            TextView myusercode;
            ImageView payimg;
            RelativeLayout addmoneylayout;


            MViewHolder(View itemView) {
                super(itemView);
                addmoneylayout = (RelativeLayout) itemView.findViewById(R.id.addmoneylayout);
                myusercode = (TextView) itemView.findViewById(R.id.myusercode);
                payimg = (ImageView) itemView.findViewById(R.id.payimg);
            }
        }
    }

    private void getPay(String rechType) {
        HttpJsonUtils.getHttpApiInstance()
                .getPay(Constants.getuseAdress() + "api/m/user/getPay", rechType, UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResGetPay>() {
                    @Override
                    protected void onCustomNext(ResGetPay resGetPay) {
                        if (resGetPay.code == 0) {
                            resgetpay.clear();
                            resgetpay.addAll(resGetPay.rech_types);
                            adapter1.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }

    //////////////////////////////////顶部列表///////////////////////////////////
    class Adapter1 extends RecyclerView.Adapter<Adapter1.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public MViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            return new MViewHolder(LayoutInflater.from(AddMoneyActivity.this)
                    .inflate(R.layout.addbackitem, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull MViewHolder mViewHolder, int i) {
            mViewHolder.textitem.setText(resgetpay.get(i).dsplay);
            if (i == postion2) {
                mViewHolder.myusercodeitem.setBackgroundResource(R.drawable.textbackground7);
                addmoneytext.setText("温馨提示:" + resgetpay.get(i).remark + "");//提交充值上方的提示
                pay_online_three = resgetpay.get(i).dsplay;
                pay_code = resgetpay.get(i).rechType;
                tiptext.setText("单笔下限" + resgetpay.get(i).min_money + "单笔上限" + resgetpay.get(i).max_money);
                xiaxian = resgetpay.get(i).min_money;
                shangxian = resgetpay.get(i).max_money;
            } else {
                mViewHolder.myusercodeitem.setBackgroundResource(R.drawable.textbackground);
            }

            mViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //输入框上方的提示
                    tiptext.setText("单笔下限" + resgetpay.get(i).min_money + "单笔上限" + resgetpay.get(i).max_money);
                    xiaxian = resgetpay.get(i).min_money;
                    shangxian = resgetpay.get(i).max_money;
                    czid = resgetpay.get(i).id + "";
                    addmoneytext.setText("温馨提示:" + resgetpay.get(i).remark + "");
                    postion2 = i;
                    pay_online_three = resgetpay.get(i).dsplay;
                    pay_code = resgetpay.get(i).rechType;
                    mViewHolder.myusercodeitem.setBackgroundResource(R.drawable.textbackground7);
                    adapter1.notifyDataSetChanged();
                }
            });
        }

        @Override
        public int getItemCount() {
            return resgetpay.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            TextView textitem;
            LinearLayout myusercodeitem;

            public MViewHolder(@NonNull View itemView) {
                super(itemView);
                textitem = itemView.findViewById(R.id.myusercode);
                myusercodeitem = itemView.findViewById(R.id.myusercodeitem);
            }
        }
    }
}
